//#include "boost/asio/io_context.hpp"
//#include "boost/asio/steady_timer.hpp"
#include <boost/filesystem.hpp>
#include <thread>
#include "mpLog.h"
#include "mpAppUtils.h"
#include "mpLocaleUtils.h"
#include "mpFileUtils.h"

#ifdef PLATFORM_WINDOWS
#include "mpWin32Func.h"
#else
#include <pwd.h>
#endif //PLATFORM_WINDOWS

namespace MP{
namespace AppUtils{

////private member
//namespace{
//} //private member

/**
 * @brief 获取当前进程Id
 * @return
 */
C_PROCESS_PID currentProcessId(){
#ifdef PLATFORM_UNIX
    return getpid();
#else
    return GetCurrentProcessId();
#endif
}

/**
 * @brief 获取当前进程对应文件的绝对路径
 * @return
 */
std::string currentProcessPath(){
    return MP::FileUtils::get_appdir();
//#if defined(PLATFORM_FREEBSD)
//    static char cmdline[PATH_MAX];
//    int mib[4];

//    mib[0] = CTL_KERN;
//    mib[1] = KERN_PROC;
//    mib[2] = KERN_PROC_ARGS;
//    mib[3] = getpid();

//    size_t len = sizeof(cmdline);
//    if (sysctl(mib, 4, &cmdline, &len, NULL, 0) == -1)
//    {
//        MPLOG_ERROR << "Could not get command line path!";
//        return "";
//    }
//    return std::string(cmdline);
//#elif defined(PLATFORM_LINUX)
//    // on Linux and Mac OS 10.6, realpath() can allocate the required
//    // amount of memory automatically, however Mac OS 10.5 does not support
//    // this, so we used a fixed-sized buffer on all platforms
//    char canonicalPathBuffer[PATH_MAX+1];
//    if (realpath(path,canonicalPathBuffer) != 0)
//    {
//        std::string path = std::string(canonicalPathBuffer);
//        MPLOG_INFO << "Current process path " << path;
//    }
//    else
//    {
//        return "";
//    }

//#elif defined(PLATFORM_MAC)
//    uint32_t bufferSize = PATH_MAX;
//    char buffer[bufferSize];
//    _NSGetExecutablePath(buffer,&bufferSize);
//    return buffer;
//#else
//    char fileName[MAX_PATH];
//    GetModuleFileNameA(0 /* get path of current process */,fileName,MAX_PATH);
//    return fileName;
//#endif
}

/**
 * @brief 当前线程等待
 * @param 等待毫秒数
 */
void currentThreadWaiting(int milliseconds){

    MPLOG_INFO << "thread [ " << std::this_thread::get_id() << " ] sleep " << milliseconds << "ms";

    std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
//    if (second < 1)
//    {
//        second = 1;
//    }

//    MPLOG_INFO << "Thread waits for " << second << " seconds";

//    boost::asio::io_context io;
//    boost::asio::steady_timer t(io, boost::asio::chrono::seconds(second));
//    t.wait();
}

/**
 * @brief 检查是否运行于cd/dvd rom下
 * @return
 */
bool currentProcessIsRuningCdRom(){
#ifdef PLATFORM_WINDOWS
    std::string dir = boost::filesystem::current_path().root_path().string();
    UINT nRes = GetDriveTypeA(dir.c_str());
    return nRes == DRIVE_CDROM;
#else
    return false;
#endif  //PLATFORM_WINDOWS
}

/**
 * @brief 检测是否有GUI用户已登录系统
 * @return
 */
void getUserLogon(std::string & useName){

    std::wstring wstr;
#ifdef PLATFORM_WINDOWS
    if(MP::Win32Func::mpsGetCurrentLogonUserName(wstr)){
        useName = MP::LocaleUtils::unicode_to_u8string(wstr);
    }

#else
    struct passwd *pwd = getpwuid(getuid());
    useName = pwd->pw_name;
#endif

}

}   //END AppUtils
}   //END MP
